import { RootState } from '@/types/store'
import { useEffect, useRef } from 'react'
import { useDispatch, useSelector } from 'react-redux'
// 自定义hook 根据所拿的action和state名发送请求并返回对应仓库的值
export default function useInitialState<StateName extends keyof RootState>(action: () => void, stateName: StateName) {
  const dispatch = useDispatch()
  const state = useSelector((state: RootState) => state[stateName])
  const actionRef = useRef(action)
  useEffect(() => {
    const actionFn = actionRef.current
    dispatch(actionFn())
  }, [dispatch])
  // console.log(state);

  return state
}
